//
//  OADOneCell.m
//  OASDK_Works
//
//  Created by ChenJie on 3/3/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADOneCell.h"
#import "OASDK.h"
#import "OADLayout.h"

@interface OADOneCell ()

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *cellAccessoryView;
@property (nonatomic, strong) UIView *separator;

@end

@implementation OADOneCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.iconView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.cellAccessoryView];
        [self.contentView addSubview:self.separator];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // calculate frame
    NSMutableDictionary *componentFrames = [NSMutableDictionary dictionary];
    CGFloat hMark = 0;
    CGFloat vMark = 0;
    for (OADLayout *layout in self.oad_layout.subLayouts) {
        NSString *componentName = layout.componentName;
        hMark += layout.marginLeft;
        vMark = layout.marginTop;
        CGRect componentFrame = CGRectMake(hMark, vMark, layout.width, layout.height);
        componentFrames[componentName] = [NSValue valueWithCGRect:componentFrame];
        hMark += layout.width;
        hMark += layout.marginRight;
    }
    
    // apply frame
    [componentFrames enumerateKeysAndObjectsUsingBlock:^(NSString *componentName, NSValue *frameValue, BOOL * _Nonnull stop) {
        UIView *component = [self valueForKey:componentName];
        if ([component isKindOfClass:UIView.class]) {
            component.frame = [frameValue CGRectValue];
//            NSLog(@"%@ frame is: %@", componentName, NSStringFromCGRect(component.frame));
        }
    }];
    
    //
    [self.contentView oa_showBounds];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods



#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Prototol methods


#pragma mark - <OADCellProtocol>

+ (OADLayout *)layoutWithViewModel:(OADOneCellViewModel *)viewModel
                  staticCellHeight:(CGFloat)staticCellHeight {
    
    OADLayout *layout = [OADLayout new];
    
    //
    OADLayout *iconViewLayout = [OADLayout new];
    iconViewLayout.componentName = @"iconView";
    iconViewLayout.marginLeft = 15;
    iconViewLayout.marginTop = 10;
    iconViewLayout.marginRight = 15;
    iconViewLayout.width = 23;
    iconViewLayout.height = 23;
    
    [layout addSubLayout:iconViewLayout];
    
    //
    OADLayout *titleLabelLayout = [OADLayout new];
    titleLabelLayout.componentName = @"titleLabel";
    titleLabelLayout.marginLeft = 15;
    titleLabelLayout.marginRight = 5;
    titleLabelLayout.marginTop = 5;
    titleLabelLayout.width = 100;
    titleLabelLayout.height = 44;
    
    [layout addSubLayout:titleLabelLayout];
    
    //
    OADLayout *accessoryIconViewLayout = [OADLayout new];
    accessoryIconViewLayout.componentName = @"cellAccessoryView";
    accessoryIconViewLayout.marginLeft = 5;
    accessoryIconViewLayout.marginTop = 0;
    accessoryIconViewLayout.marginRight = 20;
    accessoryIconViewLayout.width = 30;
    accessoryIconViewLayout.height = 30;
    
    [layout addSubLayout:accessoryIconViewLayout];
    
    return layout;
}

+ (CGFloat)cellHeightLayoutsWithViewModel:(OADOneCellViewModel *)viewModel
                                   layout:(OADLayout *)layout{
    return 100;
}

+ (void)layoutsWithViewModel:(id<OADCellViewModelProtocol>)viewModel
            staticCellHeight:(CGFloat)staticCellHeight {
    
}

+ (OADLayout *)layoutWithViewModel:(id<OADCellViewModelProtocol>)viewModel {
    return nil;
}

#pragma mark - Setter



#pragma mark - Getter

- (UIImageView *)iconView {
    if (!_iconView) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _iconView = imageView;
    }
    return _iconView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UIImageView *)cellAccessoryView {
    if (!_cellAccessoryView) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _cellAccessoryView = imageView;
    }
    return _cellAccessoryView;
}

- (UIView *)separator {
    if (!_separator) {
        UIView *view = [UIView new];
        _separator = view;
    }
    return _separator;
}

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
